﻿var url = IP + "Login";

function show() {	
	if (localStorage.getItem("loginname") != null) {
		$("#loginname").val(localStorage.getItem("loginname"));		
	}
}
setTimeout(show, 500); // 注意函数名没有引号和括弧！ 
function login() {
	url = convertURL(url);
	var loginname = $("#loginname").val();
	var pass = $("#password").val();
	if (loginname == null || loginname == "") {
		$.messager.alert('警告', '登录名不能为空！', 'warning');
		return;
	}
	if (pass == null || pass == "") {
		$.messager.alert('警告', '请输入密码！', 'warning');
		return;
	}
	if ($("#forget").is(":checked") == true) {
		localStorage.setItem("forget", "true");
		localStorage.setItem("loginname", loginname);
		localStorage.setItem("password", pass);
	}
	LoginServer(loginname, pass);
}
//转换日期格式
function LoginServer(loginname, pass) {
	$.ajax({
		type: "post", // 与可以 "get"
		url: url,
		data: {
			loginName: loginname,
			loginPasswd: pass
		},
		dataType: "jsonp",
		jsonp: "callback",
		jsonpCallback: "resultHandler",
		success: function(json) {
			if (json.result == 200) {
				/*"data":{"id":1,"empno":"1111","loginName":"778899","realName":"白勇飞","email":"1920",							 * 
				 "phoneNumber":"13362119221","telNumber":"13362119221","address":"小和山","state":1,
				 "session":"3594D72C1276029F5BC016D46A5E2DF3"}
				*/
				sessionStorage.setItem("islogin", "true");
				sessionStorage.setItem("userid", json.data.id);
				sessionStorage.setItem("empno", json.data.empno);
				sessionStorage.setItem("loginName", json.data.loginName);
				sessionStorage.setItem("realName", json.data.realName);
				sessionStorage.setItem("session", json.data.session);
				window.location = "index.html";
				insertopertlog('系统管理','系统管理','登录','',loginname);
			} else {
				$.messager.alert('登录失败', json.message, 'error');
				localStorage.setItem("forget", "false");
			}
		}
	});
}

function convertURL(url) {
	var timstamp = (new Date()).valueOf();
	if (url.indexOf("?") >= 0) {
		url = url + "&t=" + timstamp;
	} else {
		url = url + "?t=" + timstamp;
	}
	return url;
}
//记住密码，判断自动登录
function checkAutoLogin() {

	if (localStorage.getItem("loginname") != null && localStorage.getItem("password") != null && localStorage.getItem("forget") == "true") {
		url = convertURL(url);
		var loginname = localStorage.getItem("loginname");
		var pass = localStorage.getItem("password");
		LoginServer(loginname, pass);
	}
}
checkAutoLogin();